package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.BiOrderDayMapper;
import com.ygqh.baby.po.BiOrderDay;
import com.ygqh.baby.po.BiOrderDayExample;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.service.BiOrderDayService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BiOrderDayServiceImpl implements BiOrderDayService {

    @Autowired
    private BiOrderDayMapper biOrderDayMapper;

    @Override
    public List<BiOrderDay> find() {
        BiOrderDayExample example = new BiOrderDayExample();
        example.createCriteria();
        return biOrderDayMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        BiOrderDayExample example = new BiOrderDayExample();
        example.createCriteria();
        return biOrderDayMapper.countByExample(example);
    }

    @Override
    public BiOrderDay findById(Long id) {
        return biOrderDayMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(BiOrderDay biOrderDay) {
        return biOrderDayMapper.insertSelective(biOrderDay);
    }

    @Override
    public int update(BiOrderDay biOrderDay) {
        return biOrderDayMapper.updateByPrimaryKeySelective(biOrderDay);
    }

    @Override
    public ResultSet<BiOrderDay> search(QueryInfo queryInfo, Date startDate, Date endDate, String shopNo, String shopName, String warehouseNo) {
        int count = biOrderDayMapper.countSuper(startDate, endDate, shopNo, shopName, warehouseNo);
        List<BiOrderDay> list;
        if (count > 0) {
            list = biOrderDayMapper.selectSuper(queryInfo, startDate, endDate, shopNo, shopName, warehouseNo);
        } else {
            list = Collections.emptyList();
        }
        ResultSet<BiOrderDay> resultSet = new ResultSet<BiOrderDay>(count, list);
        return resultSet;
    }

    @Override
    public int saveOrUpdate(BiOrderDay biOrderDay) {
        int r = 0;
        if (biOrderDay.getId() != null) {
            r = this.update(biOrderDay);
        } else {
            r = this.save(biOrderDay);
        }
        return r;
    }

    @Override
    public List<YgOrder> findOrderByDay(Date startDate, Date endDate, String[] sourceCodes) {
        return biOrderDayMapper.selectOrderByDay(startDate, endDate, sourceCodes);
    }

    @Override
    public int addBiOrderDayBatch(List<BiOrderDay> orderDayList) {
        if (CollectionUtils.isEmpty(orderDayList)) {
            return 0;
        }
        return biOrderDayMapper.addBiOrderDayBatch(orderDayList);
    }

    @Override
    public BiOrderDay executeOrderDateGrossProfit(Date startDate, Date endDate) {
        return biOrderDayMapper.executeOrderDateGrossProfit(startDate, endDate);
    }

    @Override
    public int updateByDate(BiOrderDay profit) {
        BiOrderDayExample example = new BiOrderDayExample();
        example.createCriteria().andOrderDateEqualTo(profit.getOrderDate());
        return biOrderDayMapper.updateByExampleSelective(profit, example);
    }

    @Override
    public List<BiOrderDay> statisticsOrderBySupplier(Date startDate,
                                                      Date endDate) {
        List<BiOrderDay> list = biOrderDayMapper.executeOrderStatisticsBySupplier(startDate, endDate);
        List<BiOrderDay> listGross = biOrderDayMapper.executeOrderDateGrossProfitBySupplier(startDate, endDate);

        for (BiOrderDay d : list) {
            List<BiOrderDay> grossList = listGross.stream().filter(t -> t.getShopName().equals(d.getShopName())).collect(Collectors.toList());
            BigDecimal zero = new BigDecimal("0.00");
            if (CollectionUtils.isEmpty(grossList)) {
                d.setTotalCostPrice(zero);
                d.setGrossMargin(zero);
            } else {
                BiOrderDay gross = grossList.get(0);
                d.setTotalCostPrice(gross.getTotalCostPrice());
                if (d.getPayOrderPrice().compareTo(zero) == 0) {
                    d.setGrossMargin(zero);
                    d.setGrossMargin(zero);
                } else {
                    BigDecimal gg = (d.getPayOrderPrice().subtract(d.getTotalCostPrice()))
                            .divide(d.getPayOrderPrice(), 4, BigDecimal.ROUND_HALF_DOWN)
                            .multiply(BigDecimal.valueOf(100));
                    BigDecimal setScale = gg.setScale(2, BigDecimal.ROUND_HALF_DOWN);
                    d.setGrossMargin(setScale);
                }
            }
        }
        return list;
    }

    @Override
    public List<BiOrderDay> executeOrderStatisticsNew(Date start, Date end, String... shopNo) {
        return biOrderDayMapper.executeOrderStatisticsNew(start, end, shopNo);
    }

    @Override
    public List<BiOrderDay> executeOrderDateGrossProfitNew(Date start, Date end, String... shopNo) {
        return biOrderDayMapper.executeOrderDateGrossProfitNew(start, end, shopNo);
    }

    @Override
    public List<BiOrderDay> executeOrderStatistics(Date startDate, Date endDate) {
        return biOrderDayMapper.executeOrderStatistics(startDate, endDate);
    }

    @Override
    public BiOrderDay statisticsOrderByTime(Date startDate, Date endDate) {
        BiOrderDay bd1 = biOrderDayMapper.executeOrderStatisticsV2(startDate, endDate).get(0);
        BiOrderDay bd2 = biOrderDayMapper.executeOrderDateGrossProfitV2(startDate, endDate).get(0);
        bd1.setGrossMargin(bd2.getGrossMargin());
        bd1.setTotalCostPrice(bd2.getTotalCostPrice());
        bd1.setOrderDate(new Date());
        return bd1;
    }

    @Override
    public BiOrderDay findByShop(String shopNo, Date date) {

        return biOrderDayMapper.selectByShop(shopNo, date, "009");
    }

    @Override
    public BiOrderDay findByShop(String shopNo, Date date, String warehouseNo) {

        return biOrderDayMapper.selectByShop(shopNo, date, warehouseNo);
    }

    @Override
    public int additionBatch(List<BiOrderDay> list) {
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
            return 0;
        }
        return biOrderDayMapper.additionBatch(list);
    }

    @Override
    public List<BiOrderDay> findList(QueryInfo queryInfo, Date startDate, Date endDate, String... excludeShopNo) {
        return biOrderDayMapper.selectList(queryInfo, startDate, endDate, excludeShopNo,"0");
    }

    @Override
    public BigDecimal getTotalOrderPrice(Date startDate, Date endDate, String... excludeShopNo) {
        return biOrderDayMapper.getTotalOrderPrice(startDate, endDate, excludeShopNo, "0");
    }

    @Override
    public BigDecimal getTotalPayPriceByShop(Date startDate, Date endDate, String shopNo, String shopName, String warehouseNo) {
        return biOrderDayMapper.getTotalPayPriceByShop(startDate, endDate, shopNo, shopName, warehouseNo);
    }

    @Override
    public List<YgOrder> findOrderListAfterId(Long startId) {
        return biOrderDayMapper.selectOrderListAfterId(startId);
    }

    @Override
    public Long findOrderMaxId() {
        return biOrderDayMapper.selectOrderMaxId();
    }

    @Override
    public List<BiOrderDay> executeOrderStatisticsByDetail(Date start, Date end, String... shopNo) {
        return biOrderDayMapper.executeOrderStatisticsByDetail(start, end, shopNo);
    }

    @Override
    public int updateBatch(List<BiOrderDay> dataList) {
        if (CollectionUtils.isEmpty(dataList)) {
            return 0;
        }
        return biOrderDayMapper.updateBatch(dataList);
    }


}
